import 'package:flutter/material.dart';

abstract class BaseRecordState {
  final noteController = TextEditingController();
  bool isDisposed = false;

  void dispose() {
    if (!isDisposed) {
      noteController.dispose();
      isDisposed = true;
    }
  }

  bool get isValid;
  Map<String, dynamic> get data;
}

class FeedingState extends BaseRecordState {
  final amountController = TextEditingController();

  @override
  void dispose() {
    amountController.dispose();
    super.dispose();
  }

  @override
  bool get isValid => amountController.text.isNotEmpty;

  @override
  Map<String, dynamic> get data => {
    'feedingAmount': amountController.text,
    if (noteController.text.isNotEmpty) 'notes': noteController.text,
  };
}

class DiaperState extends BaseRecordState {
  String selectedType = 'wet';

  @override
  bool get isValid => selectedType.isNotEmpty;

  @override
  Map<String, dynamic> get data => {
    'diaperType': selectedType,
    if (noteController.text.isNotEmpty) 'notes': noteController.text,
  };
}

class TemperatureState extends BaseRecordState {
  final tempController = TextEditingController();

  @override
  void dispose() {
    tempController.dispose();
    super.dispose();
  }

  @override
  bool get isValid => tempController.text.isNotEmpty;

  @override
  Map<String, dynamic> get data => {
    'temperature': tempController.text,
    if (noteController.text.isNotEmpty) 'notes': noteController.text,
  };
}

class MedicineState extends BaseRecordState {
  final nameController = TextEditingController();
  final doseController = TextEditingController();

  @override
  void dispose() {
    nameController.dispose();
    doseController.dispose();
    super.dispose();
  }

  @override
  bool get isValid => nameController.text.isNotEmpty;

  @override
  Map<String, dynamic> get data => {
    'medicineName': nameController.text,
    'medicineDose': doseController.text,
    if (noteController.text.isNotEmpty) 'notes': noteController.text,
  };
}

class WeightState extends BaseRecordState {
  final weightController = TextEditingController();

  @override
  void dispose() {
    weightController.dispose();
    super.dispose();
  }

  @override
  bool get isValid => weightController.text.isNotEmpty;

  @override
  Map<String, dynamic> get data => {
    'weight': weightController.text,
    if (noteController.text.isNotEmpty) 'notes': noteController.text,
  };
}

class HeightState extends BaseRecordState {
  final heightController = TextEditingController();

  @override
  void dispose() {
    heightController.dispose();
    super.dispose();
  }

  @override
  bool get isValid => heightController.text.isNotEmpty;

  @override
  Map<String, dynamic> get data => {
    'height': heightController.text,
    if (noteController.text.isNotEmpty) 'notes': noteController.text,
  };
}

class SleepState extends BaseRecordState {
  DateTime? startTime;
  DateTime? endTime;

  @override
  bool get isValid => startTime != null;

  @override
  Map<String, dynamic> get data => {
    'sleepStartTime': startTime?.toIso8601String(),
    'sleepEndTime': endTime?.toIso8601String(),
    if (noteController.text.isNotEmpty) 'notes': noteController.text,
  };
}

class FoodState extends BaseRecordState {
  final nameController = TextEditingController();
  final amountController = TextEditingController();

  @override
  void dispose() {
    nameController.dispose();
    amountController.dispose();
    super.dispose();
  }

  @override
  bool get isValid => nameController.text.isNotEmpty;

  @override
  Map<String, dynamic> get data => {
    'foodName': nameController.text,
    'foodAmount': amountController.text,
    if (noteController.text.isNotEmpty) 'notes': noteController.text,
  };
}

class MilestoneState extends BaseRecordState {
  final eventController = TextEditingController();

  @override
  void dispose() {
    eventController.dispose();
    super.dispose();
  }

  @override
  bool get isValid => eventController.text.isNotEmpty;

  @override
  Map<String, dynamic> get data => {
    'milestoneEvent': eventController.text,
    if (noteController.text.isNotEmpty) 'notes': noteController.text,
  };
}

class DoctorState extends BaseRecordState {
  final hospitalController = TextEditingController();
  final reasonController = TextEditingController();

  @override
  void dispose() {
    hospitalController.dispose();
    reasonController.dispose();
    super.dispose();
  }

  @override
  bool get isValid => hospitalController.text.isNotEmpty;

  @override
  Map<String, dynamic> get data => {
    'hospitalName': hospitalController.text,
    'doctorVisitReason': reasonController.text,
    if (noteController.text.isNotEmpty) 'notes': noteController.text,
  };
}

class VaccineState extends BaseRecordState {
  final nameController = TextEditingController();
  final locationController = TextEditingController();

  @override
  void dispose() {
    nameController.dispose();
    locationController.dispose();
    super.dispose();
  }

  @override
  bool get isValid => nameController.text.isNotEmpty;

  @override
  Map<String, dynamic> get data => {
    'vaccineName': nameController.text,
    'vaccineLocation': locationController.text,
    if (noteController.text.isNotEmpty) 'notes': noteController.text,
  };
}

class CustomState extends BaseRecordState {
  final titleController = TextEditingController();
  final contentController = TextEditingController();

  @override
  void dispose() {
    titleController.dispose();
    contentController.dispose();
    super.dispose();
  }

  @override
  bool get isValid => titleController.text.isNotEmpty;

  @override
  Map<String, dynamic> get data => {
    'customTitle': titleController.text,
    'customContent': contentController.text,
    if (noteController.text.isNotEmpty) 'notes': noteController.text,
  };
}
